package com.s2.m.btchat;

import javax.bluetooth.DataElement;
import javax.bluetooth.ServiceRecord;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;

/**
 * Display the profile of a buddy
 * @author Samik
 */
public class FriendsProfile extends Form {

    /**
     * Store the username friend
     */
    private String username = "Unknown";
    /**
     * StringItem to display the full name
     */
    private StringItem fullnameItem = new StringItem("Full Name", "Unknown");
    /**
     * StringItem to display 'about me' description
     */
    private StringItem aboutMeItem = new StringItem("About", "Unknown");
    /**
     * StringItem to display the age
     */
    private StringItem ageItem = new StringItem("Age", "Unknown");
    /**
     * StringItem to display text
     */
    private StringItem sexItem = new StringItem("Sex", "Unknown");
    /**
     * StringItem to display profession
     */
    private StringItem professionItem = new StringItem("Profession",
            "Unknown");
    /**
     * keep the Service Record
     */
    ServiceRecord serviceRecord;

    public FriendsProfile() {
        super("Friend");
        addItemsToProfile();
    }

    public FriendsProfile(ServiceRecord serviceRecord) {
        super("Friend");
        addItemsToProfile();

        this.serviceRecord = serviceRecord;

        DataElement usernameDataElement =
                serviceRecord.getAttributeValue(0x4320);
        if (usernameDataElement != null && usernameDataElement.getDataType() == DataElement.STRING) {
            username = (String) usernameDataElement.getValue();
        }

        DataElement fullnameDataElement =
                serviceRecord.getAttributeValue(0x4321);
        if (fullnameDataElement != null && fullnameDataElement.getDataType() == DataElement.STRING) {
            fullnameItem.setText((String) fullnameDataElement.getValue());
        }

        DataElement aboutmeDataElement =
                serviceRecord.getAttributeValue(0x4322);
        if (aboutmeDataElement != null && aboutmeDataElement.getDataType() == DataElement.STRING) {
            aboutMeItem.setText((String) aboutmeDataElement.getValue());
        }

        DataElement ageDataElement =
                serviceRecord.getAttributeValue(0x4323);
        if (ageDataElement != null && ageDataElement.getDataType() == DataElement.STRING) {
            ageItem.setText((String) ageDataElement.getValue());
        }

        DataElement sexDataElement =
                serviceRecord.getAttributeValue(0x4324);
        if (sexDataElement != null && sexDataElement.getDataType() == DataElement.STRING) {
            sexItem.setText((String) sexDataElement.getValue());
        }

        DataElement professionDataElement =
                serviceRecord.getAttributeValue(0x4325);
        if (professionDataElement != null && professionDataElement.getDataType() == DataElement.STRING) {
            professionItem.setText((String) professionDataElement.getValue());
        }

        this.setTitle(username);
    }

    public FriendsProfile(String userName, String fullName, String aboutMe,
            String age, String sex, String profession) {
        super(userName);
        addItemsToProfile();
        fullnameItem.setText(fullName);
        aboutMeItem.setText(aboutMe);
        ageItem.setText(age);
        sexItem.setText(sex);
        professionItem.setText(profession);
    }

    public void setProfileValues(String[] value) {
        if ((value[0].trim().equals(""))) {
            setTitle(value[0]);
        }
        fullnameItem.setText(value[1]);
        aboutMeItem.setText(value[2]);
        ageItem.setText(value[3]);
        sexItem.setText(value[4]);
        professionItem.setText(value[5]);
    }

    private void addItemsToProfile() {
        this.append(fullnameItem);
        this.append(aboutMeItem);
        this.append(ageItem);
        this.append(sexItem);
        this.append(professionItem);
    }

    public String getUsername() {
        return username;
    }
}
